package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService smallVideosService;

    /**
     * 发布视频
     * 接口路径：POST
     * 请求参数：
     * videoThumbnail：封面图
     * videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        smallVideosService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = smallVideosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    // 视频点赞
    @PostMapping("/{id}/like")
    public ResponseEntity videoLikeById(@PathVariable("id") String videoId) {
        Integer count = smallVideosService.videoLikeById(videoId);
        return ResponseEntity.ok(null);
    }

    // 视频取消点赞
    @PostMapping("/{id}/dislike")
    public ResponseEntity disLikeVideo(@PathVariable("id") String videoId) {
        Integer likeCount = smallVideosService.dislikeVideo(videoId);
        return ResponseEntity.ok(likeCount);
    }

    // 视频评论列表
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") String videoId,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = smallVideosService.findVideos(videoId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    // 评论发布
    @PostMapping("/{id}/comments")
    public ResponseEntity commentsById(@PathVariable("id") String videoId, @RequestBody Map map) {
        String comment = (String) map.get("comment");
        smallVideosService.saveComments(videoId, comment);
        return ResponseEntity.ok(null);
    }

    // 视频评论点赞
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentsLike(@PathVariable("id") String videoShowId) {
        smallVideosService.likeComment(videoShowId);
        return ResponseEntity.ok(null);
    }

    // 视频评论取消点赞
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentsDislike(@PathVariable("id") String videoShowId) {
        smallVideosService.dislikeComment(videoShowId);
        return ResponseEntity.ok(null);
    }

    // 关注视频用户
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long userFocusId) {
        Boolean flag = smallVideosService.userFocus(userFocusId);
        return ResponseEntity.ok(flag);
    }

    // 取消关注用户
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long userUnFocusId) {
        smallVideosService.userUnFocus(userUnFocusId);
        return ResponseEntity.ok(null);
    }

}